package com.liuzhongni.wahahanews.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.NetworkImageView;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.activity.NewsHeadlineDetailsActivity;
import com.liuzhongni.wahahanews.headlineclass.ResponseEty;
import com.liuzhongni.wahahanews.headlineclass.ResponseEtyThird;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;

import java.util.ArrayList;

//import com.liuzhongni.wahahanews.headlineclass.MyMemoryCache;

/**
 * Created by dllo on 16/1/8.
 */
public class ViewPagerAdapterOfNewHeadline extends PagerAdapter {

    private ArrayList<View> data;
    private Context context;
    private ResponseEty ety;
    private ArrayList<ResponseEtyThird> etyThirds;
//    private MyMemoryCache cache = new MyMemoryCache();
    RequestQueue mQueue ;
//    ImageLoader imageLoader ;

    private NetworkImageView imageView;
//    private int imageViewID[]= {R.id.viewpager_iv1,R.id.viewpager_iv2,R.id.viewpager_iv3,R.id.viewpager_iv4} ;
//    private int imageViewLayout[] = {R.layout.viewpagerimage,R.layout.viewpagerimage2,R.layout.viewpageimage3,R.layout.viewpagerimage4};
    private NetworkImageView imageView3;
    private NetworkImageView imageView4;
    public ViewPagerAdapterOfNewHeadline(Context context,ArrayList<ResponseEtyThird> etyThirds,ResponseEty ety) {
        this.context = context;
        this.etyThirds = etyThirds;
        this.ety = ety;
//        mQueue = Volley.newRequestQueue(context);
//        imageLoader = new ImageLoader(mQueue,cache);
        init();
    }

    public ArrayList<View> getData() {
        return data;
    }
    public ArrayList<ResponseEtyThird> getEtyThirds() {
        return etyThirds;
    }

    private void init() {
        data = new ArrayList<>();
        for(int i = 0;i< 4;i++){
            View view = LayoutInflater.from(context).inflate(R.layout.viewpagerimage,null);
            imageView = (NetworkImageView) view.findViewById(R.id.viewpager_iv1);
            VolleySingleton.getOurInstance().imgJson(imageView,etyThirds.get(i).getImgsrc());
            data.add(view);
        }
    }


    @Override
    public int getCount() {
        if(data.size()>1){
            return Integer.MAX_VALUE;
        }
        Log.d("ViewPagerAdapterOfNewHe", "data.size():" + data.size());
        return data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        final int index = position % data.size();
        View view = data.get(index);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NewsHeadlineDetailsActivity.class);
                int num = ety.getT1348647909107().get(0).getAds().get(index).getUrl().length();
                String url = ety.getT1348647909107().get(0).getAds().get(index).getUrl().replace("|", "/").substring(4, num);
                Log.d("ViewPagerAdapterOfNewHe", url);
                intent.putExtra("url", url);
//                Bundle bundle = new Bundle();
//                bundle.putParcelableArrayList("wahaha", etyThirds);
//                intent.putExtra("hh", bundle);
                context.startActivity(intent);
            }
        });

        container.addView(view);
        Log.d("ViewPagerAdapterOfNewHe", position + "");
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        int index = position % data.size();
        View view = data.get(index);
        container.removeView(view);
    }

    //图片解析
//    private void imgJson(NetworkImageView networkImageView,  String url) {
//
//        networkImageView.setDefaultImageResId(R.mipmap.iconfont_jiazaizhong);
//        networkImageView.setErrorImageResId(R.mipmap.iconfont_jiazaishibai);
//        networkImageView.setImageUrl(url, imageLoader);
//    }
    public void updata(){
        notifyDataSetChanged();
    }
}
